﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.CodeGeneration;
using Medianamik.UI.Services;
using Medianamik.Core.Sugar;
using Medianamik.UI.Web.Services;

namespace Medianamik.UI.ConfigManagement
{
    public partial class MaintenanceTools : BasePage
    {
        private MaintenanceService maintenanceService = new MaintenanceService();

        protected void Page_Load(object sender, EventArgs e)
        {
            SetLastLocalizationUpdate();
        }

        private void SetLastLocalizationUpdate()
        {
            DateTime lastResourcesUpdate = DateTime.MinValue;
            ConfigManager.ReloadConfiguration();
            var configurations = ConfigManager.GetAllConfigurations();

            if (configurations.ContainsKey(MedianamikConfigurations.LastResourcesUpdateKey))
                DateTime.TryParse(configurations[MedianamikConfigurations.LastResourcesUpdateKey],
                                  out lastResourcesUpdate);

            ltlLocalizationUpdate.Text = lastResourcesUpdate.ToLocalTime().ToString("F", new CultureInfo("Fr-Ca"));
        }

        protected void CleanDatabaseButton_Click(object sender, EventArgs e)
        {
            new CodeService().CleanUpDatabase(); 
        }

        protected void CleanNodeInstancesButton_Click(object sender, EventArgs e)
        {
            var codeService = new CodeService();

            codeService.RemoveNeutralLangInstancesForNonNeutralLangNode(NodeTypeManager.GetAllTypes(false)
                 .Where(t => !t.IsLanguageNeutral));

            codeService.RemoveNonNeutralLangInstancesForNeutralLangNode(NodeTypeManager.GetAllTypes(false)
                 .Where(t => t.IsLanguageNeutral));
        }

        protected void RegenViewsButton_Click(object sender, EventArgs e)
        {
            var allTypes = NodeTypeManager.GetAllTypes(true)
                 .Where(t => t.IsGenerated)
                 .OrderByDescending(t => t.Depth);

            var code = new CodeService();
            allTypes.ForEach(code.GenerateView);
            code.RegenerateUserProfileView();
        }

        //protected void MagicScriptsButton_Click(object sender, EventArgs e)
        //{
        //    var allTypes = NodeTypeManager.GetAllTypes(true)
        //        .Where(t => t.IsGenerated)
        //        .OrderByDescending(t => t.Depth);

        //    CodeService code = new CodeService();
        //    allTypes.ForEach(t => code.GenerateTableAndView(t));
        //    allTypes.ForEach(t => code.FillTable(t));
        //}

        protected void LocalizationButton_Click(object sender, EventArgs e)
        {
            
        }

        protected void ReindexAllButton_Click(object sender, EventArgs e)
        {
            new Medianamik.Core.Index.Indexer().ReIndexAll(); 
        }

        protected void RebuildSiteMap_Click(object sender, EventArgs e)
        {
            ((MedianamikSiteMapProvider)SiteMap.Provider).Rebuild();
            Response.Redirect(Request.RawUrl);
        }

        protected void CleanUpInvalidUrls_Click(object sender, EventArgs e)
        {
            var report = Medianamik.Core.Urls.UrlHelper.CleanUpUrls(PublisheNodeOnCleanedUrl.Checked);

            CleanUpUrlReport.Visible = true;
            CleanUpErrors.DataSource = report.CleanUpErrors;
            CleanUpErrors.DataBind();
            InvalidUrls.Text = report.NumberOfInvalidUrls.ToString();
            CleanedUrl.Text = report.NumberOfUrlsCleaned.ToString();

        }

        protected string GetLuceneDocumentCount()
        {
            string documentCount;
            try
            {
                documentCount = Medianamik.Core.Index.Searcher.GetDocumentCount().ToString();
            }
            catch 
            {
                documentCount = "#ERROR#";
            }
            return documentCount;
        }



    }
}
